package com.htfyun;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.LruCache;
import android.util.Size;

import com.squareup.picasso.Request;

import org.geometerplus.zlibrary.ui.android.R;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sst on 2017/11/27.
 */

public enum CachePngToBmpManager {
    INSTANCE;

    //key(string) is the path of png(jpeg, jpg)
//    private Map<String, WeakReference<Bitmap>> bitmapMap = new HashMap<>();
//
//    public void putBitmap(String path, Bitmap bitmap) {
//        WeakReference<Bitmap> bitmapWeakReference = new WeakReference<Bitmap>(bitmap);
//        bitmapMap.put(path, bitmapWeakReference);
//    }
//
//    public Bitmap getBitmap(String path) {
//        WeakReference<Bitmap> bitmapWeakReference = bitmapMap.get(path);
//        return bitmapWeakReference.get();
//    }

//    private Map<String, Bitmap> bitmapMap = new HashMap<>();

    private LruCache<String, Bitmap> bitmapMap = new LruCache<>(100);

    public void putBitmap(String path, Bitmap bitmap) {
        if (path != null && bitmap != null) {
            bitmapMap.put(path, bitmap);
        }
    }

    public Bitmap getBitmap(String path) {
        return bitmapMap.get(path);
    }

    public Bitmap createBitmapByFilePath(String pngPath, int width, int height) {

        Bitmap bmp = null;
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(pngPath, options);

            if (width != 0 && height != 0) {
                calculateInSampleSize(width, height, options);
            }

            bmp = BitmapFactory.decodeFile(pngPath, options);

            putBitmap(pngPath, bmp.copy(bmp.getConfig(), false));

        } catch (Exception e) {
            e.printStackTrace();
        }

        return bmp;

    }

    private  void calculateInSampleSize(int reqWidth, int reqHeight, BitmapFactory.Options options) {
        calculateInSampleSize(reqWidth, reqHeight, options.outWidth, options.outHeight, options);
    }

    private  void calculateInSampleSize(int reqWidth, int reqHeight, int width, int height,
                                      BitmapFactory.Options options) {
        int sampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            final int heightRatio;
            final int widthRatio;
            if (reqHeight == 0) {
                sampleSize = (int) Math.floor((float) width / (float) reqWidth);
            } else if (reqWidth == 0) {
                sampleSize = (int) Math.floor((float) height / (float) reqHeight);
            } else {
                heightRatio = (int) Math.floor((float) height / (float) reqHeight);
                widthRatio = (int) Math.floor((float) width / (float) reqWidth);
                sampleSize = Math.min(heightRatio, widthRatio);
            }
        }
        options.inSampleSize = sampleSize;
        options.inJustDecodeBounds = false;
    }

}
